﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessFundsOperating
// 文件功能描述：增减款数据访问层
//
// 
// 创建标识：2012-03-07 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessFundsOperating
    {
        /// <summary>
        /// 已发所有已发卡且状态为正常的卡的数量
        /// </summary>
        /// <returns></returns>
        public int GetFundsOperatingCount()
        {
            sqldb db = new sqldb();
            string query = "select count(1) from FundsOperating_View";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 已发所有已发卡且状态为正常的卡的数量
        /// </summary>
        /// <returns></returns>
        public int GetFundsOperatingCount(string where)
        {
            sqldb db = new sqldb();
            string query = "select count(1) from FundsOperating_View where 1=1 " + where;
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public DataSet GetdeptName()
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select Dep_ID,deptName from DeptView order by Dep_ID";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }
        /// <summary>
        /// 获取增减名称列表（用于绑定下拉框，增款界面下）
        /// </summary>
        /// <returns></returns>
        public DataSet GetDepositTypeIDIncrease()
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select DepositTypeID,DepositName from Con_DepositType where Account=0 and bUse=1 and bShow=1 order by DepositTypeID";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }
        /// <summary>
        /// 获取增减名称列表（用于绑定下拉框，减款界面下）
        /// </summary>
        /// <returns></returns>
        public DataSet GetDepositTypeIDReduce()
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select DepositTypeID,DepositName from Con_DepositType where Account=1 and bUse=1 order by DepositTypeID";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 获取所有已发卡且状态为正常的卡的信息
        /// </summary>
        /// <returns></returns>
        public DataSet SelFundsOperatingInfo(int pagesize, int startindex, string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY CardNo ASC) rownumber,* " +
                               "from FundsOperating_View where 1=1 " + strquery + ") B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        public DataSet SelLeftBox(string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select * from FundsOperating_View where 1=1 " + strquery;
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 保存（给卡增款）
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int SaveIncreaseMoney(object[] obj, string[] sempall, out int countNo)
        {
            sqldb db = new sqldb();
            object objBalance;
            int decBalance;
            int count = 0;
            countNo = 0;
            for (int i = 0; i < sempall.Length; i++)
            {
                objBalance = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, "select Balance from FundsOperating_View where EmpNo='" + sempall[i] + "'");
                decBalance = Convert.ToInt32(objBalance);

                SqlParameter[] parameters = {
                    new SqlParameter("@EMPNO", SqlDbType.VarChar, 20),
                    new SqlParameter("@DEPOSIT_NAME", SqlDbType.VarChar,20),
                    new SqlParameter("@OPERATOR_VALUE",SqlDbType.Int),
                    new SqlParameter("@OLDBALANCE", SqlDbType.Int),
                    new SqlParameter("@NEWBALANCE", SqlDbType.Int),
                    new SqlParameter("@MACNO", SqlDbType.Int),
                    new SqlParameter("@OPERATORID", SqlDbType.VarChar,20),
					new SqlParameter("@RETURN",SqlDbType.VarChar,20)
                    };
                parameters[0].Value = sempall[i];
                parameters[1].Value = obj[0];
                parameters[2].Value = obj[1];
                parameters[3].Value = decBalance;
                parameters[4].Value = ParameterDirection.Output;
                parameters[5].Value = 0;
                parameters[6].Value = obj[2];
                parameters[7].Direction = ParameterDirection.Output;
                db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.StoredProcedure, "SP_ADDORDECMONEY", parameters);
                if (parameters[7].Value.ToString() == "" || parameters[7].Value == null || parameters[7].Value.ToString() == string.Empty)
                {
                    count++;
                }
                else
                {
                    countNo++;
                }
            }
            return count;
        }
        /// <summary>
        /// 保存（给卡减款）
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int SaveReduceMoney(object[] obj, string[] sempall)
        {
            //sqldb db = new sqldb();
            //string query = "update card_card set Balance=Balance-" + obj[1] + " where CardNo in(" + obj[0] + ")";
            //int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            //return rowcount;
            sqldb db = new sqldb();
            object objBalance;
            int decBalance;
            int count = 0;
            for (int i = 0; i < sempall.Length; i++)
            {
                objBalance = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, "select Balance from FundsOperating_View where EmpNo='" + sempall[i] + "'");
                decBalance = Convert.ToInt32(objBalance);

                SqlParameter[] parameters = {
                    new SqlParameter("@EMPNO", SqlDbType.VarChar, 20),
                    new SqlParameter("@DEPOSIT_NAME", SqlDbType.VarChar,20),
                    new SqlParameter("@OPERATOR_VALUE",SqlDbType.Int),
                    new SqlParameter("@OLDBALANCE", SqlDbType.Int),
                    new SqlParameter("@NEWBALANCE", SqlDbType.Int),
                    new SqlParameter("@MACNO", SqlDbType.Int),
                    new SqlParameter("@OPERATORID", SqlDbType.VarChar,20),
					new SqlParameter("@RETURN",SqlDbType.VarChar,20)
                    };
                parameters[0].Value = sempall[i];
                parameters[1].Value = obj[0];
                parameters[2].Value = obj[1];
                parameters[3].Value = decBalance;
                parameters[4].Value = ParameterDirection.Output;
                parameters[5].Value = 0;
                parameters[6].Value = obj[2];
                parameters[7].Direction = ParameterDirection.Output;
                db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.StoredProcedure, "SP_ADDORDECMONEY", parameters);
                if (parameters[7].Value.ToString() == "" || parameters[7].Value == null || parameters[7].Value.ToString() == string.Empty)
                {
                    count++;
                }
            }
            return count;
        }

        public decimal CheckMoney(string CardNoAdd)
        {
            sqldb db = new sqldb();
            string query = "select MIN(Balance) from FundsOperating_View where CardNo in(" + CardNoAdd + ")";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToDecimal(rowcount);
        }
    }
}
